package com.example.database.demo.globalException;

import com.example.database.demo.enums.AppHttpCodeEnum;
import com.example.database.demo.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;

/**
 * @author 梅芙雨Vanessa
 */
@Slf4j
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(UnsupportedEncodingException.class)
    public Result unsupportedEncodingException(UnsupportedEncodingException e) {
        String errorMessage = "An UnsupportedEncodingException occurred: " + e.getMessage();
        log.info("An UnsupportedEncodingException occurred:{} ", e.getMessage());
        return new Result(AppHttpCodeEnum.ERROR, errorMessage);
    }

    @ExceptionHandler(ResponseDataException.class)
    public Result ResponseDataException(ResponseDataException e) {
        String errorMessage = "An ResponseDataException occurred: " + e.getMessage();
        log.info("An ResponseDataException occurred:{} ", e.getMessage());
        return new Result(AppHttpCodeEnum.ERROR, errorMessage);
    }

    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerException(NullPointerException e) {
        String errorMessage = "An NullPointerException occurred: " + e.getMessage();
        log.info("An NullPointerException occurred:{} ", e.getMessage());
        return new Result(AppHttpCodeEnum.ERROR, errorMessage);
    }

    @ExceptionHandler(IOException.class)
    public Result ioException(IOException e) {
        String errorMessage = "An IOException occurred: " + e.getMessage();
        log.info("An IOException occurred:{} ", e.getMessage());
        return new Result(AppHttpCodeEnum.ERROR, errorMessage);
    }

    @ExceptionHandler(UnknownHostException.class)
    public Result unknownHostException(UnknownHostException e) {
        String errorMessage = "An unknownHostException occurred: " + e.getMessage();
        log.info("An unknownHostException occurred:{} ", e.getMessage());
        return new Result(AppHttpCodeEnum.ERROR, errorMessage);
    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeException(RuntimeException e) {
        String errorMessage = "An runtimeException occurred: " + e.getMessage();
        log.info("An runtimeException occurred:{} ", e.getMessage());
        return new Result(AppHttpCodeEnum.ERROR, errorMessage);
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        String errorMessage = "An exception occurred: " + e.getMessage();
        log.info("An exception occurred:{} ", e.getMessage());
        return new Result(AppHttpCodeEnum.ERROR, errorMessage);
    }

}
